package com.example.frn.controller;

import cn.hutool.core.bean.BeanUtil;

import com.example.frn.dto.UserInsertDto;
import com.example.frn.dto.UserModifyPasswdDto;
import com.example.frn.dto.UserPageDto;
import com.example.frn.dto.UserUpdateDto;
import com.example.frn.entity.User;

import com.example.frn.service.UserService;
import com.example.frn.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("save")
    public R save(@RequestBody UserInsertDto dto){
        User user = BeanUtil.copyProperties(dto, User.class);
        if(userService.save(user))
            return R.ok(true);
        else return R.error(false);
    }
    @PostMapping("update")
    public R update(@RequestBody UserUpdateDto dto){
        User user = BeanUtil.copyProperties(dto, User.class);
        if(userService.updateById(user))
            return R.ok(true);
        else return R.error(false);
    }
    @PostMapping("modifyPasswd")
    public R update(@RequestBody UserModifyPasswdDto dto){
        if(userService.modifyPasswd(dto))
            return R.ok(true);
        else return R.error(false);
    }
    @PostMapping("modifyNoSpeakStatus")
    public R update(@RequestBody User dto){
        User user = BeanUtil.copyProperties(dto, User.class);
        if(userService.modifyNoSpeak(user))
            return R.ok(true);
        else return R.error(false);
    }
    @PostMapping("remove/{id}")
    public R remove(@PathVariable("id") Integer id){
        if(userService.removeById(id))
            return R.ok(true);
        else return R.error(false);
    }
    @GetMapping("detail/{id}")
    public R detail(@PathVariable("id") Integer id){
        return R.ok(userService.getById(id));
    }
    @PostMapping("page")
    public R page(@RequestBody UserPageDto dto){
        return R.ok(userService.page(dto.getPageNum(),dto.getPageSize(),dto.getUsername(),dto.getNickName()));
    }

}
